﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using VoiceAI.VoiceResponse;

namespace Xiaowei.Services
{


    public class IOTService
    {
        private static readonly Lazy<IOTService> LazyInstance = new Lazy<IOTService>(() =>
        {
            return new IOTService();
        }, LazyThreadSafetyMode.ExecutionAndPublication);

        public static IOTService Instance { get { return LazyInstance.Value; } }
        private IOTService()
        {

        }
        public event EventHandler IOTRefresh;
        public Dictionary<string, IOTGroup> IOTGroups
        {
            get
            {
                return iOTGroups;
            }
        }
        public List<IOTDevice> IOTDevices
        {
            get
            {
                return iOTDevices;
            }
        }
        private readonly Dictionary<IOTType, string> iotTypeMap = new Dictionary<IOTType, string>()
        {
            {IOTType.Switch,"开关" },
            {IOTType.Light,"灯" },
            {IOTType.Aircondition,"空调" },
            {IOTType.Curtain,"窗帘" },
            {IOTType.Outlet,"插座" },
            {IOTType.Television,"电视" },
            {IOTType.Fan,"风扇" },
            {IOTType.Airpurifier,"空气净化器" },
            {IOTType.Ircontroller,"红外控制器" },
            {IOTType.Bridge,"网桥" },
            {IOTType.Ventilation,"新风" },
            {IOTType.Sweepingrobot,"扫地机器人" },
            {IOTType.Humidifier,"加湿器" },
            {IOTType.Fridge,"冰箱" },
            {IOTType.Robot,"机器人" },
            {IOTType.Washmachine,"洗衣机" },
            {IOTType.Waterdispenser,"饮水机" },
            {IOTType.Waterheater,"热水器" },
            {IOTType.Cooker,"电饭煲" },
            {IOTType.Stb,"机顶盒" },
            {IOTType.Projector,"投影仪" },
            {IOTType.Winecabinet,"酒柜" },
            {IOTType.Door,"门锁" },
            {IOTType.Dehumidifier,"除湿机" },
            {IOTType.Remoteccontrol,"遥控器" },
            {IOTType.Camera,"摄像头" },
            {IOTType.Scene,"场景" },
            {IOTType.Unknow,"未知" }
        };
        public void ResetIOTData(VoiceAI.VoiceResponse.IIOTResponseData iotResponseData)
        {
            IOTGroups.Clear();
            foreach (var item in iotResponseData.IOTGroups)
            {
                IOTGroup iOTGroup = new IOTGroup()
                {
                    Group_id = item.Key,
                    Group_name = item.Value.GroupName
                };
                IOTGroups.Add(iOTGroup.Group_id, iOTGroup);
            }
            if (iotResponseData.IOTDevices!=null)
            {
                IOTDevices.Clear();
                foreach (var item in iotResponseData.IOTDevices)
                {
                    IOTDevice iOTDevice = new IOTDevice()
                    {
                        Device_desc = iotTypeMap[item.Type],
                        Device_remark_name = item.DeviceRemarkName,
                        Device_icon= item.DeviceIcon,
                        Type= item.Type,
                        Name=iotTypeMap[item.Type],
                    };
                    string groupid = item.GroupID;
                    if (IOTGroups.ContainsKey(groupid))
                    {
                        IOTGroups[groupid].IOTDevices.Add(iOTDevice);
                    }
                    IOTDevices.Add(iOTDevice);
                }
            }
            else
            {
                XiaoweiAI.Instance.GetDeviceList();
            }
            IOTRefresh?.Invoke(null, EventArgs.Empty);
        }
        private List<IOTDevice> iOTDevices = new List<IOTDevice>();
        private Dictionary<string, IOTGroup> iOTGroups = new Dictionary<string, IOTGroup>();

    }
    public class IOTDevice
    {
        public string Device_desc
        {
            get;
            set;
        }
        public string Device_icon
        {
            ///Assets/StoreLogo.png
            get
            {
                if (string.IsNullOrEmpty(device_Icon))
                {
                    return "/Assets/IotIcon/" + Type + ".png";
                }
                return device_Icon;
            }
            set
            {
                device_Icon = value;
            }
        }
        private string device_Icon;
        public string Device_remark_name
        {
            get;
            set;
        }

        public string Name
        {
            get;
            set;
        }

        public IOTType Type { get; set; }

    }


    public class IOTGroup
    {
        public int DeviceCount
        {
            get
            {
                return IOTDevices.Count;
            }
        }
        private List<IOTDevice> iOTDevices = new List<IOTDevice>();
        public List<IOTDevice> IOTDevices => iOTDevices;
        public string Group_id
        {
            get;
            set;
        }
        public string Group_name
        {
            get;
            set;
        }
        public IOTGroup()
        {
            Group_id = "defaultID";
            Group_name = "我的设备";
        }
    }

}
